
import 'dart:ffi';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:encrypt/encrypt.dart' as ce;

class blueframe{
  blueframe({
    required this.len,
    required this.data
    }){
      this.len = len;
      this.data = data;
    }
  int type=85;
  int FC=9;
  int Seq=1;
  int len;
  List<int> data;

  static String aesEncrypted(String str,String aesKey){
    print("aesKey:${aesKey}");
    final key = ce.Key.fromUtf8(aesKey);
    final encrypt = ce.Encrypter(ce.AES(key,mode:ce.AESMode.cbc));
    final iv1 = ce.IV.fromUtf8(aesKey);
    print("before aes:${str}");
    final encrypted = encrypt.encrypt(str,iv:iv1);
    print("before base64:${encrypted.bytes}");
    return encrypted.base64.toString();
  }
  List<int> encodeData() {
    //final math = Random();
    //return [math.nextInt(255), math.nextInt(255), math.nextInt(255), math.nextInt(255)];
    String password = {'ssid':"",'password':""}.toString();
    //AES128加密
    String key = 'DG0090N000660000';
    String aesencode = aesEncrypted(password,key);
    print("aesencode:${aesencode}");

    return utf8.encode(aesencode);

  }

  List<int> to_frame(){
    Seq = Seq + 1 ;
    var framelist = [type,FC,Seq,len];
    framelist.addAll(data);
    return framelist.toList();
  }

}